/*
 * 只有用户登录才会执行这个JS
 * */

// 设置顶部用户头像和注册的天数
document.getElementById('headPortraint').src = data.imgPath;
let gap = new Date().getTime() - Number(data.registTime);
let dayGap = 0;
if(gap > 0) {
	dayGap = Math.ceil(gap / (1000 * 60 * 60 * 24));
}
document.getElementById('calendarDay').innerText = dayGap;	

/* 格式化日期 */
Date.prototype.format = function(fmt) { 
	let o = {
		"M+" : this.getMonth()+1, // 月份
　　　		"d+" : this.getDate(), // 日
　　　　	"h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, // 小时
　　　　	"H+" : this.getHours(), // 小时
　　　　	"m+" : this.getMinutes(), // 分
　　　　	"s+" : this.getSeconds(), // 秒
　　　　	"q+" : Math.floor((this.getMonth()+3)/3), // 季度
　　　　	"S" : this.getMilliseconds() // 毫秒
　　	};
　　	if(/(y+)/.test(fmt)) {
　　　　	fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
	}
	for(let k in o) {
　　　		if(new RegExp("("+ k +")").test(fmt))
　　　　		fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
	}
	return fmt;
}

let infoMenu = document.getElementById('infoMenu');
let menuData = infoMenu.querySelector('.menu-data');
let menuPublish = infoMenu.querySelector('.menu-publish');
let menuManage = infoMenu.querySelector('.menu-manage');
let menuReply = infoMenu.querySelector('.menu-reply');
let menuLike = infoMenu.querySelector('.menu-like');
let menuMy = infoMenu.querySelector('.menu-my');
let menuWant = infoMenu.querySelector('.menu-want');
let mask = document.getElementById('mask');
// 页面加载时
// 显示我的消息数
ajax({
	type: 'POST',
	url: '/chat/count',
	async: true,
	success: function(xhr) {
		let arg = JSON.parse(xhr.responseText);
		let count = Number(arg.data);
		if(count > 0) {
			let em = menuMy.children[1];
			em.style.display = 'block';
			if(count >= 100) {
				count = '99+';
			}
			em.innerText = count;
		}
	}
});
// 显示新通知
ajax({
	type: 'POST',
	url: '/notice/queryAll',
	async: true,
	success: function(xhr) {
		let arg = JSON.parse(xhr.responseText);
		if(arg.code == 3020) {
			location.href = '/page/user/login.html';
		} else if(arg.code == 200) {
			let dataArray = arg.data;
			if(!dataArray) {
				return;
			}
			let replyCount = 0;
			let likeCount = 0;
			let wantCount = 0;
			dataArray.forEach(data => {
				if(data.type) {
					let type = data.type.toString();
					if(type.startsWith("1")) {
						replyCount++;
					} else if(type.startsWith("2")) {
						likeCount++;
					} else if(type.startsWith("3")) {
						wantCount++;
					}
				}
			});
			if(replyCount > 0) {
				let em = menuReply.children[1];
				em.style.display = 'block';
				if(replyCount >= 100) {
					replyCount = '99+';
				}
				em.innerText = replyCount;
			}
			if(likeCount > 0) {
				let em = menuLike.children[1];
				em.style.display = 'block';
				if(likeCount >= 100) {
					likeCount = '99+';
				}
				em.innerText = likeCount;
			}
			if(wantCount > 0) {
				let em = menuWant.children[1];
				em.style.display = 'block';
				if(wantCount >= 100) {
					wantCount = '99+';
				}
				em.innerText = wantCount;
			}
		}
	}
});
/*
	点击menu后设置本item为active
*/
function setActive(ele) {
	removeStyle(infoMenu, 'active');
	ele = ele || this;
	ele.className = ele.className + ' active';
}
/*
	功能：使所有子元素隐藏
*/
function setHidden(parentNode) {
	for(let i = 0; i < parentNode.children.length; i++) {
		let ele = parentNode.children[i];
		ele.style.display = 'none';
	}
}
/*
	功能：页面所有输入框获取焦点，失去焦点事件
*/
function inputFBEvent(inputs) {
	for(let i = 0; i < inputs.length; i++) {
		let input = inputs[i];
		input.onfocus = function() {
			input.style.borderColor = '#00a1d6';
		}
		input.onblur = function() {
			input.style.borderColor = '#bfcbd9';
		}
	}
}
/*
	功能：留言板获取焦点事件
*/
function focusEvent() {
	this.style.borderColor = '#549ec5';
}
/*
	功能：留言板失去焦点事件
*/
function blurEvent() {
	this.style.borderColor = '#e5e9ef';
}
/*
	功能：获取鼠标在页面上的位置
*/ 
function getPage(e) {
	var pageX = e.pageX || e.clientX + getScroll().scrollLeft;
	var pageY = e.pageY || e.clientY + getScroll().scrollTop;
	return {
		pageX: pageX,
		pageY: pageY
	}
}
/*
	功能：target区域模拟滚动条
	参数1: 内容区域和滚动栏的父元素
	参数2：内容区域
	参数3：滚动栏
	参数4：滚动条
	注：box的高度要与content的高度一致
*/
function scrollBarTarget(box, content, scroll, bar) {
	// 滚动条的长度 / 滚动栏的长度 = 内容显示的长度 / 内容实际的长度
	if(box.clientHeight < content.scrollHeight) {
		let length = scroll.clientHeight * box.clientHeight / content.scrollHeight;
		bar.style.height = length + 'px';
	}
	let isMouseDown = false;
	box.onmousemove = function() {
		bar.style.opacity = 1;
	}
	box.onmouseleave = function() {
		if(!isMouseDown) {
			bar.style.opacity = 0;
		}
	}
	
	box.onmousewheel = function(e) {
		box.style.transition = 'top 200ms';
		content.style.transition = 'top 200ms';
		e = e || window.event;
		let isUp = false;
		let distance = 80;
		if (e.wheelDelta) { //判断浏览器IE，谷歌滑轮事件
			if (e.wheelDelta > 0) { //当滑轮向上滚动时
				isUp = true;
			}
			if (e.wheelDelta < 0) { //当滑轮向下滚动时
				isUp = false;
			}
		} else if (e.detail) { //Firefox滑轮事件
			if (e.detail> 0) { //当滑轮向上滚动时
				isUp = true;
			}
			if (e.detail< 0) { //当滑轮向下滚动时
				isUp = false;
			}
		}
		if(isUp) {
			distance = -80;
		}
		let index = bar.style.top.indexOf('px');
		let barY = Number(bar.style.top.substring(0, index)) + distance;
		barY = barY < 0 ? 0 : barY;
		barY = barY > (scroll.clientHeight - bar.clientHeight) ? (scroll.clientHeight - bar.clientHeight) : barY;
		bar.style.top = barY + 'px';

		// 改变内容的位置
		// 内容滚动的距离 / 内容能最大滚动的距离 = 滚动条滚动的距离 / 滚动条最大能滚动的距离
		let contentMax = content.scrollHeight - box.clientHeight;
		let barMax = scroll.clientHeight - bar.clientHeight;
		let contentY = contentMax * barY / barMax;
		content.style.top = -contentY + 'px';
	}
	bar.onmousedown = function(e) {
		box.style.transition = '';
		content.style.transition = '';
		isMouseDown = true;
		e = e || window.event;
		// 鼠标在滚动条的位置
		let y = getPage(e).pageY - box.offsetTop - bar.offsetTop;
		document.onmousemove = function(e) {
			e = e || window.event;
			let barY = getPage(e).pageY - box.offsetTop - y;
			barY = barY < 0 ? 0 : barY;
			barY = barY > (scroll.clientHeight - bar.clientHeight) ? (scroll.clientHeight - bar.clientHeight) : barY;
			bar.style.top = barY + 'px';

			// 拖拽滚动条时，改变内容的位置
			// 内容滚动的距离 / 内容能最大滚动的距离 = 滚动条滚动的距离 / 滚动条最大能滚动的距离
			let contentMax = content.scrollHeight - box.clientHeight;
			let barMax = scroll.clientHeight - bar.clientHeight;
			let contentY = contentMax * barY / barMax;
			content.style.top = -contentY + 'px';
		}
	}
	document.onmouseup = function(e) {
		document.onmousemove = null;
		isMouseDown = false;
		e = e || window.event;
		if(bar.style.opacity == 1) {
			if(getPage(e).pageX > (box.offsetLeft + box.clientWidth) || getPage(e).pageY > (box.offsetTop + box.clientHeight)) {
				bar.style.opacity = 0;
			}
		}
	}
}
/*
	功能：chat区域模拟滚动条
*/
function scrollBarChat(box, content, scroll, bar) {
	// 滚动条的长度 / 滚动栏的长度 = 内容显示的长度 / 内容实际的长度
	if(box.clientHeight < content.scrollHeight) {
		let length = scroll.clientHeight * box.clientHeight / content.scrollHeight;
		bar.style.height = length + 'px';
	}
	bar.style.transition = '';
	content.style.transition = '';
	// 内容和滚动条默认在最下方
	let cMax = content.scrollHeight - box.clientHeight;
	content.style.top = -cMax + 'px';
	let bMax = scroll.clientHeight - bar.clientHeight;
	bar.style.top = bMax + 'px';
	box.onmousewheel = function(e) {
		bar.style.transition = 'top 200ms';
		content.style.transition = 'top 200ms';
		e = e || window.event;
		let isUp = false;
		let distance = 33;
		if (e.wheelDelta) { //判断浏览器IE，谷歌滑轮事件
			if (e.wheelDelta > 0) { //当滑轮向上滚动时
				isUp = true;
			}
			if (e.wheelDelta < 0) { //当滑轮向下滚动时
				isUp = false;
			}
		} else if (e.detail) { //Firefox滑轮事件
			if (e.detail> 0) { //当滑轮向上滚动时
				isUp = true;
			}
			if (e.detail< 0) { //当滑轮向下滚动时
				isUp = false;
			}
		}
		if(isUp) {
			distance = -33;
		}
		let index = bar.style.top.indexOf('px');
		let barY = Number(bar.style.top.substring(0, index)) + distance;
		barY = barY < 0 ? 0 : barY;
		barY = barY > (scroll.clientHeight - bar.clientHeight) ? (scroll.clientHeight - bar.clientHeight) : barY;
		bar.style.top = barY + 'px';

		// 改变内容的位置
		// 内容滚动的距离 / 内容能最大滚动的距离 = 滚动条滚动的距离 / 滚动条最大能滚动的距离
		let contentMax = content.scrollHeight - box.clientHeight;
		let barMax = scroll.clientHeight - bar.clientHeight;
		let contentY = contentMax * barY / barMax;
		content.style.top = -contentY + 'px';
	}
	bar.onmousedown = function(e) {
		bar.style.transition = '';
		content.style.transition = '';
		e = e || window.event;
		// 鼠标在滚动条的位置
		let y = getPage(e).pageY - box.offsetTop - bar.offsetTop;
		document.onmousemove = function(e) {
			e = e || window.event;
			let barY = getPage(e).pageY - box.offsetTop - y;
			barY = barY < 0 ? 0 : barY;
			barY = barY > (scroll.clientHeight - bar.clientHeight) ? (scroll.clientHeight - bar.clientHeight) : barY;
			bar.style.top = barY + 'px';

			// 拖拽滚动条时，改变内容的位置
			// 内容滚动的距离 / 内容能最大滚动的距离 = 滚动条滚动的距离 / 滚动条最大能滚动的距离
			let contentMax = content.scrollHeight - box.clientHeight;
			let barMax = scroll.clientHeight - bar.clientHeight;
			let contentY = contentMax * barY / barMax;
			content.style.top = -contentY + 'px';
		}
	}
	document.onmouseup = function() {
		document.onmousemove = null;
	}
}
/*
	功能：menuData点击事件
*/
let infoDesc = document.getElementById('infoDesc');
function menuDataClick() {
	setActive(menuData);
	let infoData = infoDesc.children[0];
	setHidden(infoDesc);
	infoData.style.display = 'block';
	removeAllChildren(mask);
	mask.style.display = 'none';
	// 填入用户数据
	let dataLis = infoData.querySelectorAll('li');
	// 设置学号
	if(!data.sno) {
		// 若没有sno则跳转至登录页面
		location.replace('/page/user/login.html');
		return;
	}
	dataLis[0].children[1].innerText = data.sno;
	// 设置昵称
	if(!data.nickname) {
		// 若没有nickname则跳转至登录页面
		location.replace('/page/user/login.html');
		return;
	}
	let nicknameEle = dataLis[1].children[1];
	nicknameEle.value = data.nickname;
	// 设置联系电话
	let phoneNumberEle = dataLis[2].children[1];
	phoneNumberEle.value = data.phoneNumber;
	// 设置院系
	let departmentValue = data.department;
	if(departmentValue) {
		let departmentOptions = dataLis[3].querySelectorAll('option');
		for(let i = 0; i < departmentOptions.length; i++) {
			let departmentOption = departmentOptions[i];
			if(departmentOption.value == departmentValue) {
				departmentOption.selected = 'selected';
				departmentOptions[0].selected = '';
				break;
			}
		}
	}
	// 设置QQ号
	if(data.qno) {
		dataLis[4].children[1].value = data.qno;
	}
	// 设置班级
	if(data.classname) {
		dataLis[5].children[1].value = data.classname;
	}
	// 设置寝室号
	if(data.dormitory) {
		dataLis[6].children[1].value = data.dormitory;
	}
	// 设置头像
	infoData.querySelector('.fileup img').src = data.imgPath;
	
	// 输入框
	let inputs = infoData.querySelectorAll('input');
	inputFBEvent(inputs);
	let button = infoData.querySelector('button');
	phoneNumberEle.onblur = function() {
		let regExp = /^1[345678]\d{9}$/;
		let value = this.value;
		let tip = this.nextElementSibling;
		if(regExp.test(value)) {
			tip.style.display = 'none';
		}
		this.style.borderColor = '#bfcbd9';
	}
	nicknameEle.onblur = function() {
		this.style.borderColor = '#bfcbd9';
		if(this.value.trim().length > 0) {
			this.nextElementSibling.style.display = 'none';	
		}
	}
	
	// 提交按钮点击事件
	button.onclick = function() {
		let number = Number(infoData.querySelector('.number em').innerText);
		if(number != data.sno) {
			return;
		}
		let flag = true;
		// 校验有没有写昵称
		if(nicknameEle.value.trim().length == 0) {
			let i = nicknameEle.nextElementSibling;
			i.style.display = 'block';
			i.innerText = '必填项';
			nicknameEle.style.borderColor = '#f00';
			flag = false;
		} else {
			nicknameEle.nextElementSibling.style.display = 'none';
			nicknameEle.style.borderColor = '#bfcbd9';
		}

		// 校验手机号码
		let regExp = /^1[345678]\d{9}$/;
		let phoneNumberValue = phoneNumberEle.value;
		let tip = phoneNumberEle.nextElementSibling;
		if(phoneNumberValue.trim().length == 0) {	
			tip.style.display = 'block';
			tip.innerText = '必填项';
			phoneNumberEle.style.borderColor = '#f00';
			flag = false;
		} else if(!regExp.test(phoneNumberValue)) {
			tip.style.display = 'block';
			tip.innerText = '手机号码有误';
			phoneNumberEle.style.borderColor = '#f00';
			flag = false;
		} else {
			tip.style.display = 'none';
			phoneNumberEle.style.borderColor = '#bfcbd9';
		}
		// 校验有没有选择院系
		let selectEle = infoData.querySelector('.department select');
		let optionIndex = selectEle.selectedIndex;
		if(optionIndex == 0) {
			let i = selectEle.nextElementSibling;
			i.style.display = 'block';
			i.innerText = '必填项';
			flag = false;
		} else {
			selectEle.nextElementSibling.style.display = 'none';
		}
		if(flag) {
			// 跳转
			ajax({
				type: 'POST',
				url: '/user/modifyInfo',
				async: true,
				data: {
					nickname: encodeURIComponent(nicknameEle.value.trim()),
					phoneNumber: phoneNumberValue,
					department: selectEle.children[optionIndex].value,
					qno: dataLis[4].children[1].value.trim(),
					classname: encodeURIComponent(dataLis[5].children[1].value.trim()),
					dormitory: encodeURIComponent(dataLis[6].children[1].value.trim())
				},
				beforeSend: function() {
					// 显示等待图片
					mask.style.display = 'block';
					mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 50%; left: 50%; margin-top: -45px;margin-left: -45px;width: 90px;height: 90px;background: url(/images/loading2.gif) no-repeat;background-size: 90px;">
									  </div>`;
				},
				success: function(xhr) {
					let arg = JSON.parse(xhr.responseText);
					removeAllChildren(mask);
					mask.style.display = 'none';
					if(arg.code == 200 && arg.message == 'success') {
						toast('修改成功！', '#84cc77')
						return;
					} else if(arg.code == 300) {
						// 格式不正确
						toast('信息填写格式有误');
					} else if(arg.code == 3020){
						// 登录状态过期
						mask.style.display = 'block';
						mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 50%; left: 50%; margin-top: -76.5px;margin-left: -195px;width: 390px;background-color: #fff;border-radius: 4px;border: 1px solid #ddd;box-shadow: 0 0 7px #eee;">
							<div class="mask-h clearfix">
								<span style="float: left;width: 34px;height: 34px;background: url(/images/tip_icon.png) no-repeat;margin-top: 10px;margin-left: 10px;"></span>
								<span style="float: left;height: 53px;line-height: 53px;padding-left: 10px;font-size: 14px;">请先登录</span>
								<span style="float: right;width: 24px;height: 24px;background: url(/images/x_icon.png) no-repeat;cursor: pointer;margin-top: 9px;margin-right: 8px;" title="关闭面板" ></span>
							</div>
							<div class="mask-content" style="padding: 5px 22px;font-size: 14px;">您的登录状态已过期，请重新登录！</div>
							<div class="mask-b clearfix" style="margin: 25px 0 17px;">
								<button class="mask-button mask-button-cancel" style="float: right; margin-right: 29px;">关闭</button>
								<button class="mask-button" style="float: right; margin-right: 12px;">确定</button>
							</div>
			  			</div>`;
						let maskH = mask.querySelector('.mask-h');
						maskH.children[2].onclick = function() {
							// 顶部关闭icon
							removeAllChildren(mask);
							mask.style.display = 'none';
						}
						let maskB = mask.querySelector('.mask-b');
						maskB.children[0].onclick = function() {
							// 关闭 
							removeAllChildren(mask);
							mask.style.display = 'none';
						}
						maskB.children[1].onclick = function() {
							// 确定
							location.href = '/page/user/login.html';
							return;
						};
					} else {
						// 其他
						location.href = '/page/error500.html';
					}
				}
			})
		}
	}
	// 提交用户头像
	let inputUp = infoDesc.querySelector('.fileup input');
	// 显示提示信息
	let param = location.search;
	// 在不刷新页面的情况下改变地址栏内容
	window.history.replaceState('', '', '/page/user/space/');	
	if(param) {
		let msg = param.substring(1);
		if(msg == 'e') {
			toast('支持的图片格式为JPG和PNG，大小不能超过2M', 3000);
		} else {
			toast('更换成功', '#84cc77', 3000);
		}
	}
	inputUp.onchange = function() {
		let value = this.value;
		if(value.length > 0) {
			this.parentNode.parentNode.submit();
		}
	} 
}
/*
	功能：menuPublish点击事件
*/
function menuPublishClick() {
	setActive(menuPublish);
	let infoPublish = infoDesc.children[1];
	setHidden(infoDesc);
	infoPublish.style.display = 'block';
	removeAllChildren(mask);
	mask.style.display = 'none';
	// 输入框颜色
	let inputs = infoPublish.querySelectorAll('input');
	let textarea = infoPublish.querySelector('textarea');
	inputFBEvent(inputs);
	textarea.onfocus = function() {
		this.style.borderColor = '#00a1d6';
	}
	textarea.onblur = function() {
		this.style.borderColor = '#bfcbd9';
	}
	
	// 从服务器获取session中的图片路径
	let imgWrap = infoPublish.querySelector('.upload-wrap');
	removeAllChildren(imgWrap);
	ajax({
		type: 'POST',
		async: true,
		url: '/goods/getTemp',
		beforeSend: function() {
			// 显示等待图片
			mask.style.display = 'block';
			mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 50%; left: 50%; margin-top: -45px;margin-left: -45px;width: 90px;height: 90px;background: url(/images/loading2.gif) no-repeat;background-size: 90px;">
							  </div>`;
		},
		success: function(xhr) {
			removeAllChildren(mask);
			mask.style.display = 'none';
			let arg = JSON.parse(xhr.responseText);
			if(arg.code == 200) {
				if(arg.data) {
					let paths = arg.data.split(';');
					for(let index in paths) {
						if(paths[index]) {
							let div = document.createElement('div');
							div.className = 'upload-item';
							div.innerHTML = `<img />
											<span></span>`;
							div.children[0].src = '' + paths[index];
							imgWrap.appendChild(div);
							div.onmousemove = function() {
								this.children[1].style.display = 'block';
							}
							div.onmouseleave = function() {
								this.children[1].style.display = 'none';
							}
							div.children[1].onclick = function() {
								ajax({
									type: 'POST',
									async: true,
									url: '/goods/removeImg',
									data: {
										imgPath: div.children[0].src,
									},
									beforeSend: function() {
										// 显示等待图片
										mask.style.display = 'block';
										mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 50%; left: 50%; margin-top: -45px;margin-left: -45px;width: 90px;height: 90px;background: url(/images/loading2.gif) no-repeat;background-size: 90px;">
														  </div>`;
									},
									success: function(xhr) {
										removeAllChildren(mask);
										mask.style.display = 'none';
										let argu = JSON.parse(xhr.responseText);
										if(argu.code == 200) {
											removeWorld(div);
										} else {
											toast('请刷新页面后重试');
										}
										
									}
								});
							}
						}

					}
				}
			}
		}
	});
	let publishHref = location.href;
	window.history.replaceState('', '', '/page/user/space/#/publish');	
	if(publishHref.endsWith('?c')) {
		toast('最多只能上传5张图片');
	} else if(publishHref.endsWith('?e')){
		toast('支持的图片格式为JPG和PNG，大小不能超过2M', 3000);
	}
	
	
	// 上传图片
	let inputUp = infoPublish.querySelector('.choose-wrap input');
	inputUp.onchange = function() {
		let value = this.value;
		if(value.length > 0) {
			this.parentNode.parentNode.submit();
		}
	} 

	// 商品标签
	/*
		功能：商品标签取消事件
	*/
	let labelSure = infoPublish.querySelector('.label-sure');
	let labelWait = infoPublish.querySelector('.publish-label-wait');
	let labelUl = labelWait.children[1];
	function labelCancelEvent() {
		let valueStr = this.previousElementSibling.innerText;
		let parent = this.parentNode;
		labelSure.removeChild(parent);
		// 添加到待选标签处
		if(labelWait.style.display == 'none') {
			labelWait.style.display = 'block';
		}
		let ele = document.createElement('li');
		ele.innerText = valueStr;
		labelUl.appendChild(ele);
		// 待选标签点击事件
		ele.onclick = labelWaitEvent;
	}
	let labelCancel = infoPublish.querySelector('.label-sure i');
	labelCancel.onclick = labelCancelEvent;

	// 待选标签
	/*
		功能：待选标签点击事件
	*/
	function labelWaitEvent() {
		if(labelSure.children.length < 5) {
			let content = this.innerText;
			labelUl.removeChild(this);
			let ele = document.createElement('strong');
			ele.innerHTML = `<em>${content}</em>
							<i class="iconfont icon-cuo"></i>`
			labelSure.appendChild(ele);
			// 给i注册点击事件
			ele.children[1].onclick = labelCancelEvent;
		}
		// 若没有待选标签则隐藏这一项
		if(lis.length == 0) {
			labelWait.style.display = 'none';
		}
	}
	let lis = labelUl.children;
	for(let i = 0; i< lis.length; i++) {
		let li = lis[i];
		lis[i].onclick = labelWaitEvent;
	}

	// 自定义标签输入框回车Enter事件
	let input = infoPublish.querySelector('.create-label');
	input.onkeydown = function(e) {
		// 回车键
		if(e.keyCode == 13) {
			let valueStr = this.value.trim();
			if(valueStr.length > 0) {
				this.value = '';
				if(labelSure.children.length < 5) {
					// 添加到商品标签处
					let ele = document.createElement('strong');
					ele.innerHTML = `<em>${valueStr}</em>
						<i class="iconfont icon-cuo"></i>`;
					labelSure.appendChild(ele);
					// 给i注册点击事件
					ele.children[1].onclick = labelCancelEvent;
				} else {
					// 添加到待选标签处
					if(labelWait.style.display == 'none') {
						labelWait.style.display = 'block';
					}
					let ele = document.createElement('li');
					ele.innerText = valueStr;
					labelUl.appendChild(ele);
					// 待选标签点击事件
					ele.onclick = labelWaitEvent;
				}
				
			}
			
		}
	}

	// 发布按钮点击事件
	let button = infoPublish.querySelector('button');
	button.onclick = function() {
		let uploadImgs = infoPublish.querySelector('.upload-wrap');
		let goodsName = infoPublish.querySelector('.publish-name input');
		let goodsPrice = infoPublish.querySelector('.publish-price input');
		let goodsCount = infoPublish.querySelector('.publish-count input');
		if(uploadImgs.children.length == 0 || goodsName.value.length == 0 ||
			textarea.value.length == 0 || goodsCount.value.trim().length ==0 ||
			goodsPrice.value.length == 0 || labelSure.children.length == 0) {
			toast('信息填写不完整');
			return;
		}
		if(goodsName.value.startsWith(' ') || goodsName.value.endsWith(' ')) {
			toast('商品名称前后不能有空格');
			return;
		} 
		if(textarea.value.length < 15) {
			toast('商品详情至少15个字');
			return;
		} 
		let regExp = /^(([1-9][0-9]*)|0)(\.[0-9]+)?$/;
		if(!regExp.test(goodsPrice.value)) {
			toast('商品价格填写有误');
			return;
		}
		if(goodsPrice.value == 0) {
			toast('商品价格不能为0');
			return;
		}
		if(!/^[1-9]\d?$/.test(goodsCount.value)) {
			toast('商品数量有误');
			return;
		}
		// 信息填写无误
		let labelEms = labelSure.querySelectorAll('em');
		let labelValue = '';
		for(let j = 0; j < labelEms.length; j++) {
			let str = labelEms[j].innerText;
			if(j == labelEms.length -1) {
				labelValue = labelValue + str;
			} else {
				labelValue = labelValue + str + '####TPL####';
			}
		}
		// 发送请求
		ajax({
			type: 'POST',
			async: true,
			url: '/goods/submitProduct',
			data: {
				name: encodeURIComponent(goodsName.value),
				price: goodsPrice.value,
				details: encodeURIComponent(textarea.value),
				count: goodsCount.value,
				label: encodeURIComponent(labelValue)
			},
			beforeSend: function() {
				// 显示等待图片
				mask.style.display = 'block';
				mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 50%; left: 50%; margin-top: -45px;margin-left: -45px;width: 90px;height: 90px;background: url(/images/loading2.gif) no-repeat;background-size: 90px;">
								  </div>`;
			},
			success: function(xhr) {
				removeAllChildren(mask);
				let arg = JSON.parse(xhr.responseText);
				if(arg.code == 3100) {
					mask.style.display = 'none';
					toast('请刷新页面后重试');
				} else if(arg.code == 3101) {
					mask.style.display = 'none';
					toast('请完善您的个人信息后再发布');
				} else if(arg.code == 300) {
					mask.style.display = 'none';
					toast('商品信息格式不正确');
				} else if(arg.code == 200) {
					toast('商品提交成功', '#84cc77');
					setTimeout(() => {
						location.href = '/page/user/space/#/manage';
						location.reload(true);
					}, 2000);
				}
			}
		});
	}
}
/*
	功能：menuManage点击事件
*/
function menuManageClick() {
	setActive(menuManage);
	let infoManage= infoDesc.children[2];
	setHidden(infoDesc);
	infoManage.style.display = 'block';
	removeAllChildren(mask);
	mask.style.display = 'none';
	let input = infoManage.querySelector('input');
	input.onfocus = function() {
		input.style.borderColor = '#00a1d6';
	}
	input.onblur = function() {
		input.style.borderColor = '#bfcbd9';
	}
	let manageHref = location.href;
	window.history.replaceState('', '', '/page/user/space/#/manage');	
	if(manageHref.endsWith('?s')) {
		toast('下架成功', '#84cc77');
	}
	// Ajax请求数据
	ajax({
		type: 'POST',
		async: true,
		url: '/goods/myGoods',
		beforeSend: function() {
			// 显示等待图片
			mask.style.display = 'block';
			mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 50%; left: 50%; margin-top: -45px;margin-left: -45px;width: 90px;height: 90px;background: url(/images/loading2.gif) no-repeat;background-size: 90px;">
							  </div>`;
		},
		success: function(xhr) {
			removeAllChildren(mask);
			mask.style.display = 'none';
			let arg = JSON.parse(xhr.responseText);
			if(arg.code == 3020) {
				location.href = '/page/user/login.html';
			} else if(arg.code == 200) {
				let dataArray = arg.data;
				let goodsWrap = infoManage.querySelector('.goods-wrap');
				if(!dataArray || dataArray.length == 0) {
					// 页面默认显示无数据背景图
					return;
				}
				// 有数据
				let goodsMask = goodsWrap.nextElementSibling;
				goodsMask.style.display = 'none';
				// 设置manage-state的商品数量
				let goodsState = infoManage.querySelector('.manage-state');
				let goodsAll = goodsState.children[0]; // 全部商品
				let goodsReview = goodsState.children[1]; // 审核中
				let goodsPass = goodsState.children[2]; // 已通过
				let goodsNoPass= goodsState.children[3]; // 未通过
				let goodsOff = goodsState.children[4]; // 已下架
				goodsAll.children[1].innerText = dataArray.length;
				let review = 0;
				let pass = 0;
				let noPass = 0;
				let off = 0;
				function stateAllClick(content) {
					removeStyle(goodsState, 'on');
					this.className = 'on';
					removeAllChildren(goodsWrap);
					let _iconStyle = "";
					dataArray.forEach((data) => {
						if(content) {
							if(data.name.indexOf(content) != -1 || data.details.indexOf(content) != -1) {
							} else {
								return;
							}
						}
						let _offStyle = 'display: block';
						let _url = 'javascript:void(0);';
						if(data.state == 0) {
							// 默认是审核中
							review++;
							_iconStyle = "background: url(/images/review_icon.png) no-repeat;";
						}else if(data.state == 1) {
							pass++;
							_iconStyle = "background: url(/images/pass_icon.png) no-repeat;"
							_url = '/page/user/search/desc.html?' + data.gid;
						}else if(data.state == 2) {
							noPass++;
							_iconStyle = "background: url(/images/no_pass_icon.png) no-repeat;"
						}else if(data.state == 3) {
							off++;
							_iconStyle = "height:33px;background: url(/images/offshelf_icon.png) no-repeat;"
							_offStyle = 'display: none';
						}
						let goodsItem = document.createElement('div');
						goodsItem.className='goods-item clearfix';
						let _imgPath = data.imgPath;
						let imgPathIndex = _imgPath.indexOf(';')
						if(imgPathIndex != -1) {
							_imgPath = "//127.0.0.1:8080" + _imgPath.substring(0, imgPathIndex);
						}
						let _time = new Date(data.time).format("yyyy-MM-dd HH:mm");
						goodsItem.innerHTML = `<a href="${_url}" class="fl goods-img">
												<img src="${_imgPath}" alt="发布的商品图片" />
											</a>
											<div class="fl goods-desc">
												<a href="${_url}" class="goods-title" title="${data.name}">${data.name}</a>
												<strong>${data.realPrice}</strong>
												<p>
													<a href="${_url}" title="${data.details}">${data.details}</a>
												</p>
												<span class="goods-label clearfix">
													<i class="state-icon" style="${_iconStyle}"></i>
												</span>
												<div class="goods-info">
													<span class="goods-view">浏览量：${data.view}</span>
													<span>发布时间：${_time}</span>
												</div>
											</div>
											<a href="/goods/${data.gid}/undercarriage" class="fr goods-cancel" style="${_offStyle}" title="当商品已全部预约时会自动下架">
												<i class="iconfont icon-xiajia"></i>
												<span>下架</span>
											</a>`;
						let goodsLabel = goodsItem.querySelector('.goods-label');
						let labels = data.label.split('####TPL####');
						labels.forEach((label) => {
							let labelDiv = document.createElement('div');
							labelDiv.innerText = label;
							goodsLabel.appendChild(labelDiv);
						});
						goodsWrap.appendChild(goodsItem);
					});
				}
				stateAllClick.call(goodsAll);
				goodsAll.onclick = function() {
					stateAllClick.call(goodsAll);
				}
				goodsReview.children[1].innerText =review;
				goodsPass.children[1].innerText =pass;
				goodsNoPass.children[1].innerText =noPass;
				goodsOff.children[1].innerText =off;
				function stateClick(state) {
					removeStyle(goodsState, 'on');
					this.className = 'on';
					removeAllChildren(goodsWrap);
					dataArray.forEach((data) => {
						if(data.state == state) {
							let goodsItem = document.createElement('div');
							goodsItem.className='goods-item clearfix';
							let _imgPath = data.imgPath;
							let imgPathIndex = _imgPath.indexOf(';');
							if(imgPathIndex != -1) {
								_imgPath = '//127.0.0.1:8080' + _imgPath.substring(0, imgPathIndex);
							}
							let _iconStyle = 'background: url(/images/review_icon.png) no-repeat;'
							let _offStyle = 'display: block';
							let _url = 'javascript:void(0);';
							if(state == 1) {
								_iconStyle = 'background: url(/images/pass_icon.png) no-repeat;'
								_url = '/page/user/search/desc.html?' + data.gid;
							} else if(state == 2) {
								_iconStyle = 'background: url(/images/no_pass_icon.png) no-repeat;'
							} else if(state == 3) {
								_iconStyle = 'height:33px;background: url(/images/offshelf_icon.png) no-repeat;'
								_offStyle = 'display: none';
							}
							let _time = new Date(data.time).format("yyyy-MM-dd HH:mm");
							goodsItem.innerHTML = `<a href="${_url}" class="fl goods-img">
												<img src="${_imgPath}" alt="发布的商品图片" />
											</a>
											<div class="fl goods-desc">
												<a href="${_url}" title="${data.name}">${data.name}</a>
												<strong>${data.realPrice}</strong>
												<p>
													<a href="${_url}">${data.details}</a>
												</p>
												<span class="goods-label clearfix">
													<i class="state-icon" style="${_iconStyle}"></i>
												</span>
												<div class="goods-info">
													<span class="goods-view">浏览量：${data.view}</span>
													<span>发布时间：${_time}</span>
												</div>
											</div>
											<a href="/goods/${data.gid}/undercarriage" class="fr goods-cancel" style="${_offStyle}" title="当商品已全部预约时会自动下架">
												<i class="iconfont icon-xiajia"></i>
												<span>下架</span>
											</a>`;
							let goodsLabel = goodsItem.querySelector('.goods-label');
							let labels = data.label.split('####TPL####');
							labels.forEach((label) => {
								let labelDiv = document.createElement('div');
								labelDiv.innerText = label;
								goodsLabel.appendChild(labelDiv);
							});
							goodsWrap.appendChild(goodsItem);
						}
					});
				}
				if(review > 0) {
					goodsReview.onclick = function() {
						stateClick.call(goodsReview, 0);
					}
				}
				if(pass > 0) {
					goodsPass.onclick = function() {
						stateClick.call(goodsPass, 1);
					}
				}
				if(noPass > 0) {
					goodsNoPass.onclick = function() {
						stateClick.call(goodsNoPass, 2);
					}
				}
				if(off > 0) {
					goodsOff.onclick = function() {
						stateClick.call(goodsOff, 3);
					}
				}
				// 搜索商品
				let goodsSearch = goodsState.nextElementSibling;
				let searchInput = goodsSearch.children[0];
				let searchIcon = goodsSearch.children[1];
				function search() {
					let value = searchInput.value;
					stateAllClick(value.trim());
				}
				searchIcon.onclick = search;
			}
		}
	});
	
}
/*
	功能：回复我的点击事件
*/
function menuReplyClick() {
	setActive(menuReply);
	let infoReply = infoDesc.children[3];
	setHidden(infoDesc);
	infoReply.style.display = 'block';
	removeAllChildren(mask);
	mask.style.display = 'none';
	// 在不刷新页面的情况下改变地址栏内容
	window.history.replaceState('', '', '/page/user/space/#/reply');
	let replyWrap = infoReply.querySelector('.reply-wrap');
	removeAllChildren(replyWrap);
	ajax({
		type: 'POST',
		url: '/notice/queryByType',
		async: true,
		data: {
			type: "1"
		},
		beforeSend: function() {
			// 显示等待图片
			mask.style.display = 'block';
			mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 85px; left: 410px;width: 50px;height: 50px;background: url(/images/loading2.gif) no-repeat;background-size: 50px;">
							  </div>`;
		},
		success: function(xhr) {
			removeAllChildren(mask);
			mask.style.display = 'none';
			let arg = JSON.parse(xhr.responseText);
			if(arg.code == 3020) {
				location.href = '/page/user/login.html';
			} else if(arg.code == 200) {
				console.log(arg.data);
				let dataArray = arg.data;
				// 若没有数据则显示无数据背景图，不向下执行
				if(!dataArray) {
					let replyMask = replyWrap.nextElementSibling;
					replyMask.style.display = 'block';
					return;
				}
				dataArray.forEach(data => {
					if(!data.noticeItem) {
						return;
					}
					let div = document.createElement('div');
					div.className = 'reply-item clearfix';
					let _time = new Date(data.noticeItem.time).format("yyyy-MM-dd HH:mm");
					div.innerHTML = `<img src="${data.noticeItem.imgPath}" alt="用户头像" />
							<div class="reply-meta">
								<div class="fl">
									<div class="reply-title">
										<span class="reply-name">${data.noticeItem.nickname}</span>
										<span>${data.noticeItem.title}</span>
									</div>
									<p class="reply-content">${data.noticeItem.content}</p>
									<div class="reply-b">
										<span class="reply-time">${_time}</span>
										<div class="reply-delete">
											<i class="iconfont icon-shanchu"></i>
											<span>删除该通知</span>
										</div>
									</div>
								</div>
								<div class="fr reply-r">
									<a href="javascript:void(0);">${data.noticeItem.itemName}</a>
								</div>
							</div>`;
					replyWrap.appendChild(div);
					let deleteIcon = div.querySelector('.reply-delete');
					div.onclick = function() {
						location.href = data.noticeItem.url;
					}
					div.onmousemove = function() {
						deleteIcon.style.display = 'inline-block';
						deleteIcon.onclick = function(e) {
							e = e || window.event;
							e.cancelBubble = true; // 防止触发外层div点击事件
							mask.style.display = 'block';
							mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 50%; left: 50%; margin-top: -76.5px;margin-left: -200px;width: 400px;height: 153px;box-sizing: border-box;border-radius: 4px;padding: 16px 20px;overflow: hidden; background-color: #fff;border: 1px solid #ddd;box-shadow: 0 0 7px #eee;">
												<div class="mask-h clearfix">
													<span style="float: left;font-size: 18px;color: #23ade5;">删除</span>
													<span style="float: right;width: 24px;height: 24px;background: url(/images/x_icon.png) no-repeat;cursor: pointer;" title="关闭面板" ></span>
												</div>
												<div class="mask-content" style="padding: 25px 0; font-size: 14px;">删除该条通知后将无法恢复，是否继续？</div>
												<div class="mask-b">
													<button class="mask-button mask-button-cancel" style="float: right; margin-right: 4px;">取消</button>
													<button class="mask-button" style="float: right; margin-right: 12px;">确定</button>
												</div>
											  </div>`;
							let maskH = mask.querySelector('.mask-h');
							maskH.children[1].onclick = function() {
								removeAllChildren(mask);
								mask.style.display = 'none';
							}
							let maskB = mask.querySelector('.mask-b');
							maskB.children[0].onclick = function() {
								removeAllChildren(mask);
								mask.style.display = 'none';
							}
							maskB.children[1].onclick = function() {
								// 向后端发送删除请求
								ajax({
									type: 'POST',
									url: '/notice/delete',
									async: true,
									data: {
										id: data.nid
									},
									beforeSend: function() {
										removeAllChildren(mask);
										mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 85px; left: 410px;width: 50px;height: 50px;background: url(/images/loading2.gif) no-repeat;background-size: 50px;">
											  </div>`;
									},
									success: function(xhr) {
										removeAllChildren(mask);
										mask.style.display = 'none';
										let arg1 = JSON.parse(xhr.responseText);
										if(arg.code == 200) {
											toast('删除成功', '#84cc77');
											removeWorld(div);
											if(replyWrap.children.length == 0) {
												replyWrap.nextElementSibling.style.display = 'block';
											}
										} else if(arg.code == 3020) {
											location.href = '/page/user/login.html';
										} else {
											toast('系统繁忙，请刷新页面后重试');
										}
									}
								});
							}
						}
					}
					div.onmouseout = function() {
						deleteIcon.style.display = 'none';
						deleteIcon.onclick = null;
					}
				});
				let em = menuReply.children[1];
				if(em.style.display == 'block') {
					em.style.display = 'none';
					em.innerText = '';
				}
			} else {
				toast('系统繁忙请稍后重试');
			}
		}
	});
}
/*
	功能：收到的赞点击事件
*/
function menuLikeClick() {
	setActive(menuLike);
	let infoLike = infoDesc.children[4];
	setHidden(infoDesc);
	infoLike.style.display = 'block';
	removeAllChildren(mask);
	mask.style.display = 'none';
	// 在不刷新页面的情况下改变地址栏内容
	window.history.replaceState('', '', '/page/user/space/#/like');	
	
	let likeWrap = infoLike.querySelector('.like-wrap');
	removeAllChildren(likeWrap);
	ajax({
		type: 'POST',
		url: '/notice/queryByType',
		async: true,
		data: {
			type: "2"
		},
		beforeSend: function() {
			// 显示等待图片
			mask.style.display = 'block';
			mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 85px; left: 410px;width: 50px;height: 50px;background: url(/images/loading2.gif) no-repeat;background-size: 50px;">
							  </div>`;
		},
		success: function(xhr) {
			removeAllChildren(mask);
			mask.style.display = 'none';
			let arg = JSON.parse(xhr.responseText);
			if(arg.code == 3020) {
				location.href = '/page/user/login.html';
			} else if(arg.code == 200) {
				console.log(arg.data);
				let dataArray = arg.data;
				// 若没有数据则显示无数据背景图，不向下执行
				if(!dataArray) {
					let likeMask = likeWrap.nextElementSibling;
					likeMask.style.display = 'block';
					return;
				}
				dataArray.forEach(data => {
					if(!data.noticeItem) {
						return;
					}
					let div = document.createElement('div');
					div.className = 'like-item clearfix';
					let _time = new Date(data.noticeItem.time).format("yyyy-MM-dd HH:mm");
					div.innerHTML = `<img src="${data.noticeItem.imgPath}" alt="用户头像" />
							<div class="like-meta">
								<div class="fl">
									<div class="like-title">
										<span class="like-name">${data.noticeItem.nickname}</span>
										<span>${data.noticeItem.title}</span>
									</div>
									<div class="like-b">
										<span class="like-time">${_time}</span>
										<div class="like-delete">
											<i class="iconfont icon-shanchu"></i>
											<span>删除该通知</span>
										</div>
									</div>
								</div>
								<div class="fr like-r">
									<a href="javascript:void(0);">${data.noticeItem.itemName}</a>
								</div>
							</div>`;
					likeWrap.appendChild(div);
					let deleteIcon = div.querySelector('.like-delete');
					div.onclick = function() {
						location.href = data.noticeItem.url;
					}
					div.onmousemove = function() {
						deleteIcon.style.display = 'inline-block';
						deleteIcon.onclick = function(e) {
							e = e || window.event;
							e.cancelBubble = true; // 防止触发外层div点击事件
							mask.style.display = 'block';
							mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 50%; left: 50%; margin-top: -86.5px;margin-left: -200px;width: 400px;height: 173px;box-sizing: border-box;border-radius: 4px;padding: 16px 20px;overflow: hidden; background-color: #fff;border: 1px solid #ddd;box-shadow: 0 0 7px #eee;">
									<div class="mask-h clearfix">
										<span style="float: left;font-size: 18px;color: #23ade5;">删除</span>
										<span style="float: right;width: 24px;height: 24px;background: url(/images/x_icon.png) no-repeat;cursor: pointer;" title="关闭面板" ></span>
									</div>
									<div class="mask-content" style="padding: 25px 0; font-size: 14px;">该条通知删除后，当有新点赞时会重新出现在列表，是否继续？</div>
									<div class="mask-b">
										<button class="mask-button mask-button-cancel" style="float: right; margin-right: 4px;">取消</button>
										<button class="mask-button" style="float: right; margin-right: 12px;">确定</button>
									</div>
								  </div>`;
							let maskH = mask.querySelector('.mask-h');
							maskH.children[1].onclick = function() {
								removeAllChildren(mask);
								mask.style.display = 'none';
							}
							let maskB = mask.querySelector('.mask-b');
							maskB.children[0].onclick = function() {
								removeAllChildren(mask);
								mask.style.display = 'none';
							}
							maskB.children[1].onclick = function() {
								// 向后端发送删除请求
								ajax({
									type: 'POST',
									url: '/notice/delete',
									async: true,
									data: {
										id: data.nid
									},
									beforeSend: function() {
										removeAllChildren(mask);
										mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 85px; left: 410px;width: 50px;height: 50px;background: url(/images/loading2.gif) no-repeat;background-size: 50px;">
											  </div>`;
									},
									success: function(xhr) {
										removeAllChildren(mask);
										mask.style.display = 'none';
										let arg1 = JSON.parse(xhr.responseText);
										if(arg.code == 200) {
											location.reload(true);
										} else if(arg.code == 3020) {
											location.href = '/page/user/login.html';
										} else {
											toast('系统繁忙，请刷新页面后重试');
										}
									}
								});
							}
						}
					}
					div.onmouseout = function() {
						deleteIcon.style.display = 'none';
						deleteIcon.onclick = null;
					}
				});
				let em = menuLike.children[1];
				if(em.style.display == 'block') {
					em.style.display = 'none';
					em.innerText = '';
				}
			} else {
				toast('系统繁忙请稍后重试');
			}
		}
	});
}
/*
	功能：我的消息点击事件
*/
function menuMyClick() {
	setActive(menuMy);
	let infoMy = infoDesc.children[5];
	setHidden(infoDesc);
	infoMy.style.display = 'block';
	removeAllChildren(mask);
	mask.style.display = 'none';
	// 在不刷新页面的情况下改变地址栏内容
	window.history.replaceState('', '', '/page/user/space/#/my');	
	
	let targetBox = infoMy.querySelector('.target-box');
	let targetContent = targetBox.children[0];
	let targetScroll = targetBox.children[1];
	let targetBar = targetScroll.children[0];
	// 右侧chat
	let chatBox = infoMy.querySelector('.chat-box');
	let chatContent = chatBox.children[0];
	let chatScroll = chatBox.children[1];
	let chatBar = chatScroll.children[0];
	let textarea = infoMy.querySelector('textarea');
	let chatMask = infoMy.querySelector('.nodata-mask');
	// 先清除子元素
	chatMask.style.display = 'block';
	removeAllChildren(targetContent);
	removeAllChildren(chatContent);
	// Ajax请求数据，往targetContent添加数据，添加最近打开的聊天窗口的数据
	ajax({
		type: 'POST',
		url: '/chat/findRecent',
		async: true,
		beforeSend: function() {
			targetBox.previousElementSibling.innerText = '近期消息 （同步中，请稍后） ';
		},
		success: function(xhr) {
			targetBox.previousElementSibling.innerText = '近期消息';
			let arg = JSON.parse(xhr.responseText);
			if(arg.code == 3020) {
				location.href = '/page/user/login.html';
			} else if(arg.code == 200) {
				let dataArray = arg.data;
				for(let i = 0; i < dataArray.length; i++) {
					let data = dataArray[i];
					let nextData = null;
					if(i < dataArray.length - 1) {
						nextData = dataArray[i + 1];
					}
					// 聊天内容
					let chatItemArray = data.chatItemList;
					let _newCount = chatItemArray.length - data.readCount;
					let item = document.createElement('div');
					item.className = 'target-item';
					item.innerHTML = `<div class="fl target-img">
										<img src="${data.notMe.imgPath}" alt="聊天对象头像" />
									</div>
									<div class="fl target-info">
										<span>${data.notMe.nickname}</span>
										<p>${data.recentContent}</p>
									</div>
									<i>×</i>`;
					// 与该聊天人的新信息数
					if(_newCount > 0) {
						let em = document.createElement('em');
						em.className = 'target-new-count';
						if(_newCount > 100) 
							_newCount = '99+';
						em.innerText = _newCount;
						item.appendChild(em);
					}
					// 只有第一个才可以为当前显示的聊天对象
					let chatName = infoMy.querySelector('.chat-name');
					if(i == 0 && data.isOpen == 1) {
						item.className = item.className + ' on';
						chatName.innerText = data.notMe.nickname;
						// 右侧显示当前聊天对象的聊天内容
						addChatContent(data);
						// 设置chat滚动条
						scrollBarChat(chatBox, chatContent, chatScroll, chatBar);
						// 删除新消息标记
						if(_newCount > 0) {
							item.removeChild(item.children[3]);
						}
						ajax({
							type: 'POST',
							url: '/chat/clearNew',
							async: true,
							data: {
								id: data.notMe.uid
							},
							success: function(xhr) {
								let arg = JSON.parse(xhr.responseText);
								if(arg.code == 3020) {
									location.href = '/page/user/login.html';
								} 
							}
						});
					}
					// 添加事件
					let itemClose = item.children[2];
					item.onmousemove = function() {
						itemClose.style.left = '3px';
					}
					item.onmouseleave = function() {
						itemClose.style.left = '-80px';
					}
					item.onclick = function() {
						removeStyle(this.parentNode, 'on');
						this.className = this.className + ' on';
						// 右侧顶部显示当前聊天对象的昵称
						chatName.innerText = data.notMe.nickname;
						// 删除chatContent里的内容
						removeAllChildren(chatContent);
						// 显示新的聊天内容
						addChatContent(data);
						// 设置chat滚动条
						scrollBarChat(chatBox, chatContent, chatScroll, chatBar);
						chatMask.style.display = 'none';
						// 删除新消息标记
						if(this.children.length == 4) {
							this.removeChild(this.children[3]);
						}
						ajax({
							type: 'POST',
							url: '/chat/clearNew',
							async: true,
							data: {
								id: data.notMe.uid
							},
							success: function(xhr) {
								let arg = JSON.parse(xhr.responseText);
								if(arg.code == 3020) {
									location.href = '/page/user/login.html';
								} 
							}
						});
						// 标记当前为open
						ajax({
							type: 'POST',
							url: '/chat/setOpen',
							async: true,
							data: {
								id: data.notMe.uid
							},
							success: function(xhr) {
								let arg = JSON.parse(xhr.responseText);
								if(arg.code == 3020) {
									location.href = '/page/user/login.html';
								} 
							}
						});
					}
					itemClose.onclick = function(e) {
						e = e || window.event;
						e.cancelBubble = true; // 取消事件冒泡，防止执行item.onclick
						// 若删除当前聊天对象，则默认显示下一个聊天对象
						if(item.className.lastIndexOf('on') != -1) {
							let nextEle = item.nextElementSibling;
							// 删除chatContent里的内容
							removeAllChildren(chatContent);
							if(nextEle) {
								nextEle.className = nextEle.className + ' on';
								// 右侧顶部显示当前聊天对象的昵称
								let name = nextEle.querySelector('.target-info span').innerText;
								chatName.innerText = name;
								// 显示新的聊天内容
								if(nextData) {
									addChatContent(nextData);
									// 设置chat滚动条
									scrollBarChat(chatBox, chatContent, chatScroll, chatBar);
								}
							} else {
								// 若是最后一个元素，删除后显示对应图片
								chatMask.style.display = 'block';
							}
						} 
						removeWorld(this.parentNode);
						// 设置target滚动条
						scrollBarTarget(targetBox, targetContent, targetScroll, targetBar);
						ajax({
							type: 'POST',
							url: '/chat/clearRecent',
							async: true,
							data: {
								id: data.notMe.uid
							},
							success: function(xhr) {
								let arg = JSON.parse(xhr.responseText);
								if(arg.code == 3020) {
									location.href = '/page/user/login.html';
								} 
							}
						});
					}
					targetContent.appendChild(item);
				}
				let em = menuMy.children[1];
				if(em.style.display == 'block') {
					em.style.display = 'none';
					em.innerText = '';
				}
				// 若没有，不向下执行代码。
				if(targetContent.children.length == 0) {
					return;
				} 
				chatMask.style.display = 'none';

				// 设置target滚动条
				scrollBarTarget(targetBox, targetContent, targetScroll, targetBar);
				
				let button = infoMy.querySelector('button');
				let indicatorSpan = textarea.nextElementSibling.children[0];
				textarea.oninput = function(e) {
					e = e || window.event;
					let value = this.value;
					// 设置字数
					indicatorSpan.innerText = value.length;
					if(value.length >= 500 && e.keyCode != 8) {
						return false;
					}
					if(value.length > 0) {
						button.style.backgroundColor = '#1389bf';
						button.style.borderColor = '#1389bf';
						button.style.color = '#fff';
					} else {
						button.style.backgroundColor = '#fff';
						button.style.borderColor = '#e7e7e7';
						button.style.color = '#000';
					}
				}
				textarea.focus(); // 页面加载时默认textarea获取焦点
				// button按钮点击事件
				button.onclick = function() {
					let value = textarea.value;
					if(value.length > 0) {
						chatBar.style.transition = '';
						chatContent.style.transition = '';
						let div = document.createElement('div');
						div.className = 'chat-item is-me';
						div.innerHTML = `<img src="/images/my.jpg" />
										<div class="chat-message">${value}</div>`;
						chatContent.appendChild(div);
						// 还原输入框状态
						textarea.value = '';
						indicatorSpan.innerText = 0;
						button.style.backgroundColor = '#fff';
						button.style.borderColor = '#e7e7e7';
						button.style.color = '#000';
						// 内容更新到左侧内容处
						let targetInfoContent = targetContent.querySelector('.on .target-info p');
						targetInfoContent.innerText = value;
						if(chatBox.clientHeight < chatContent.scrollHeight) {
							let length = chatScroll.clientHeight * chatBox.clientHeight / chatContent.scrollHeight;
							chatBar.style.height = length + 'px';
						}
						chatBar.style.top = (chatScroll.clientHeight - chatBar.clientHeight) + 'px';
						let contentMax = chatContent.scrollHeight - chatBox.clientHeight;
						chatContent.style.top = -contentMax + 'px';
					}
				}
				function addChatContent(data) {
					let chatItemArray = data.chatItemList;
					// 传过来是降序
					chatItemArray.reverse();
					for(let i = 0; i < chatItemArray.length; i++) {
						let chatItem = chatItemArray[i];
						if(i == 0) {
							let div = document.createElement('div');
							div.className = 'chat-time';
							let time = new Date(chatItem.time).format('yyyy年MM月dd日 HH:mm');
							div.innerHTML = `<span>${time}</span>`;
							chatContent.appendChild(div);
						}
						let div = document.createElement('div');
						let _headPath = '';
						let _nickname = '';
						let _department = '';
						let _phoneNumber = '';
						if(chatItem.sendId == data.me.uid) {
							div.className = 'chat-item is-me  clearfix';
							_headPath = data.me.imgPath;
							_nickname = data.me.nickname;
							_department = data.me.department;
							_phoneNumber = data.me.phoneNumber;
						} else {
							div.className = 'chat-item not-me  clearfix';
							_headPath = data.notMe.imgPath;
							_nickname = data.notMe.nickname;
							_department = data.notMe.department;
							_phoneNumber = data.notMe.phoneNumber;
							
						}
						if(chatItem.iid) {
							// 订单
							let _imgPath = chatItem.orderItem.goods.imgPath;
							let imgPathIndex = _imgPath.indexOf(';')
							if(imgPathIndex != -1) {
								_imgPath = _imgPath.substring(0, imgPathIndex);
							}
							div.innerHTML = `<img src="${_headPath}" />
									<div class="chat-message clearfix" style="background-color: #fff;">
										<div class="chat-order">
											<span class="order-tip">${data.recentContent}（共${chatItem.orderItem.count}件）</span>
											<div class="clearfix" style="background-color: #f9f9f9;">
												<img src="${_imgPath}">
												<div class="order-info">
												<span class="order-name">${chatItem.orderItem.goods.name}</span>
												<strong>¥${chatItem.orderItem.goods.realPrice}</strong>
												<p>${chatItem.orderItem.goods.details}</p>
												</div>
											</div>
											<div class="order-user">
												<span>昵称：<i>${_nickname}</i></span>
												<span>院系：<i>${_department}</i></span>
												<span>电话：<i>${_phoneNumber}</i></span>
											</div>
										</div>
									</div>`;
						} else {
							div.innerHTML = `<img src="${_headPath}" />
										<div class="chat-message">${chatItem.content}</div>`;
						}
						chatContent.appendChild(div);
					}
					textarea.focus();
				}
			}
		}
	});
	
}
/*
	功能：求购消息点击事件
*/
function menuWantClick() {
	setActive(menuWant);
	let infoWant = infoDesc.children[6];
	setHidden(infoDesc);
	infoWant.style.display = 'block';
	removeAllChildren(mask);
	mask.style.display = 'none';
	// 在不刷新页面的情况下改变地址栏内容
	window.history.replaceState('', '', '/page/user/space/#/want');	
	/* 留言区域效果 */
	let commentArea = document.getElementById('commentArea');
	removeAllChildren(commentArea)
	ajax({
		type: 'POST',
		url: '/want/getWants',
		async: true,
		data: {
			id: 'true'
		},
		beforeSend: function() {
			// 显示等待图片
			mask.style.display = 'block';
			mask.innerHTML = `<div class="mask-wrap" style="position: absolute; top: 85px; left: 410px;width: 50px;height: 50px;background: url(/images/loading2.gif) no-repeat;background-size: 50px;">
							  </div>`;
		},
		success: function(xhr) {
			removeAllChildren(mask);
			mask.style.display = 'none';
			let arg3 = JSON.parse(xhr.responseText);
			if(arg3.code == 200) {
				// 若没有数据则显示无数据背景图，不向下执行
				let wants = arg3.data;
				if(!wants) {
					let wantMask = commentArea.nextElementSibling;
					wantMask.style.display = 'block';
					return;
				}
				setWants(wants);
				let em = menuWant.children[1];
				if(em.style.display == 'block') {
					em.style.display = 'none';
					em.innerText = '';
				}
			} else {
				toast('系统繁忙，请刷新页面后重试');
			}
		}
		
	});
	
	/*
	 * 	添加求购信息
	 * 	参数1：留言集合
	 * 
	 * */
	function setWants(wants) {
		wants.forEach(want => {
			// 添加求购信息
			let div = document.createElement('div');
			div.className = 'comment-item';
			let _time = new Date(want.time).format('yyyy-MM-dd');
			let _imgPath = '/images/face/default.jpg';
			div.innerHTML = `<div class="clearfix">
								<div class="item-l fl">
									<img src="${want.user.imgPath}" alt="用户头像" />
								</div>
								<div class="item-r fl">
									<span class="item-name">${want.user.nickname}</span>
									<span class="item-time">${_time}</span>
									<p class="item-content">${want.content}</p>
									<div class="item-info">
										<span class="item-like">${want.likeCount}</span>
										<span class="item-reply">${want.replyCount}</span>
									</div>
								</div>
							</div>
							<div class="comment-more" style="display:none;">
								<div class="comment-publish clearfix">
									<div class="publish-l">
										<img src="${_imgPath}" alt="用户头像" />
									</div>
									<div class="publish-m">
										<textarea cols="80" name="msg" rows="5" placeholder="回复 @${want.user.nickname}："></textarea>
									</div>
									<button class="publish-submit">发表评论</button>
								</div>
								<div class="comment-reply clearfix">
									<ul>
									</ul>
								</div>
							</div>`;
			commentArea.appendChild(div);
			// 给点赞和回复注册事件
			div.querySelector('.item-like').onclick = function() {
				likeButtonEvent.call(this, want.wid);
			}
			div.querySelector('.item-reply').onclick = function() {
				replyButtonEvent.call(this, want.wid);
			};
		});
	}
	
	/*
	功能：点击点赞按钮事件
	参数1：求购信息id
	 */
function likeButtonEvent(id) {
	let _this = this;
	if(this.style.background.indexOf('click') == -1) {
		ajax({
			type: 'POST',
			url: '/want/like',
			async: true,
			data: {
				id: id,
			},
			success: function(xhr) {
				let arg = JSON.parse(xhr.responseText);
				if(arg.code == 200) {
					_this.style.background = 'url(/images/like_click_icon.png) no-repeat';
					let count = Number(_this.innerText);
					_this.innerText = count + 1;
				} else if(arg.code == 3020) {
					toast('请先登录');
				} else {
					toast('系统繁忙，请刷新页面后重试');
					setTimeout(() => {
						location.reload(true);
					}, 3000);
				}
			}
		});
	}
}
/*
	功能：点击评论按钮事件
*/
function replyButtonEvent(id) {
	// 设置评论图标
	if(!this.style.backgroundImage || this.style.backgroundImage == 'url("/images/message_icon.png")') {
		this.style.background = 'url(/images/message_blue_icon.png) no-repeat';
	} else {
		this.style.background = 'url(/images/message_icon.png) no-repeat';
	}
	let publish = this.parentNode.parentNode.parentNode.nextElementSibling;
	let button = publish.querySelector('button');
	let _this = this;
	if(publish.style.display.indexOf('none') != -1) {
		publish.style.display = 'block';
		let commentReply = publish.children[1];
		let commentReplyUl = commentReply.children[0];
		if(!data) {
			// 未登录
			let publishM = publish.children[0].children[1];
			removeAllChildren(publishM);
			let div = document.createElement('div');
			div.className = 'publish-login-wrap';
			div.innerHTML = `请先<a class="publish-tologin" href="/page/user/login.html" target="_blank">登录</a>后发表评论(・ω・)`;
			publishM.appendChild(div);
			button.style = 'background-color: #e5e9ef;cursor: default;color: #b8c0cc;';
		} else {
			// 添加用户头像
			publish.querySelector('img').src = data.imgPath;
			let textarea = publish.querySelector('textarea');
			textarea.onfocus = focusEvent;
			textarea.onblur = blurEvent; 
			textarea.focus();
			button.onclick = function() {
				let contentStr = textarea.value.trim();
				if(contentStr.length > 0 && button.innerText != '发送中') {
					console.log(contentStr);
					ajax({
						type: 'POST',
						url: '/reply/addByWant',
						async: true,
						data: {
							id: id,
							content: encodeURIComponent(contentStr)
						},
						beforeSend: function() {
							button.style = 'background-color: #e5e9ef;cursor: default;color: #b8c0cc;padding: 0;';
							button.innerText = '发送中';
							textarea.disabled = 'disabled';
						},
						success: function(xhr) {
							// 还原之前的效果
							button.style = '';
							button.innerText = '发表评论';
							textarea.disabled = '';
							// 清空留言板的内容
							textarea.value = '';
							let arg = JSON.parse(xhr.responseText);
							if(arg.code == 3400) {
								toast('系统繁忙，请刷新页面后重试');
								setTimeout(() => {
									location.reload(true);
								}, 3000);
							} else if(arg.code == 3020) {
								location.href = '/page/user/login.html';
							} else if(arg.code == 300) {
								toast('请输入评论内容');
							} else if(arg.code == 200) {
								// 发布成功效果
								let div = document.createElement('div');
								div.className = 'publish-suc';
								div.innerText = '发布成功';
								publish.appendChild(div);
								setTimeout(() => {
									div.remove();
								}, 2000);
								// 添加回复
								if(commentReply.style.display == 'none') {
									commentReply.style.display = 'block';
								}
								let li = document.createElement('li');
								li.className = 'clearfix';
								let _time = new Date(arg.data.time).format('yyyy-MM-dd HH:mm');
								li.innerHTML = `<div class="content-img">
													<img src="${data.imgPath}" alt="用户头像" />
												</div>
												<div class="content-main">
													<span class="content-name">${data.nickname}</span>
													<p>${contentStr}</p>
													<div class="content-b">
														<span class="content-time">${_time}</span>
														<span class="content-delete">删除</span>
													</div>
												</div>`;
								if(commentReplyUl.children.length == 0) {
									commentReplyUl.appendChild(li);
								} else {
									commentReplyUl.insertBefore(li, commentReplyUl.children[0]);
								}
								// 回复数加1
								let count = Number(_this.innerText);
								_this.innerText = count + 1;
								// 设置点击删除事件
								li.querySelector('.content-delete').onclick = function() {
									// 这里未做删除提示
									ajax({
										type: 'POST',
										url: '/reply/deleteByWant',
										async: true,
										data: {
											id1: arg.data.rid,
											id2: id
										},
										success: function(xhr) {
											let arg2 = JSON.parse(xhr.responseText);
											if(arg2.code == 3400) {
												toast('系统繁忙，请刷新页面后重试');
												setTimeout(() => {
													location.reload(true);
												}, 3000);
											} else if(arg2.code == 3020) {
												location.replace('/page/user/login.html');
											} else if(arg2.code == 200) {
												removeWorld(li);
												toast('删除成功', '#84cc77');
												let count = Number(_this.innerText);
												_this.innerText = count - 1;
											}
										}
									});
								}
							}
						}
					});
				}	
			}
		}
		// 请求回复信息
		let loadingDiv = document.createElement('div');
		ajax({
			type: 'POST',
			url: '/reply/findReplys',
			async: true,
			data: {
				id: id
			},
			beforeSend: function() {
				loadingDiv.style = 'margin: 0 0 22px 90px;color: #aaa;font-family: fangsong;font-size: 16px;';
				loadingDiv.innerText = 'loading...';
				publish.appendChild(loadingDiv);
				removeAllChildren(commentReplyUl);
			},
			success: function(xhr) {
				loadingDiv.remove();
				let arg3 = JSON.parse(xhr.responseText);
				if(arg3.code == 200) {
					let replys = arg3.data;
					if(replys) {
						replys.forEach(reply => {
							let replyLi = document.createElement('li');
							replyLi.className = 'clearfix';
							let _time = new Date(reply.time).format('yyyy-MM-dd HH:mm');
							replyLi.innerHTML = `<div class="content-img">
												<img src="${reply.user.imgPath}" alt="用户头像" />
											</div>
											<div class="content-main">
												<span class="content-name">${reply.user.nickname}</span>
												<p>${reply.content}</p>
												<div class="content-b">
													<span class="content-time">${_time}</span>
												</div>
											</div>`;
							if(data && reply.user.uid == data.uid) {
								let deleteSpan = document.createElement('span');
								deleteSpan.className = 'content-delete';
								deleteSpan.innerText = '删除';
								replyLi.querySelector('.content-b').appendChild(deleteSpan);
								// 点击删除事件
								deleteSpan.onclick = function() {
									// 这里未做删除提示
									ajax({
										type: 'POST',
										url: '/reply/deleteByWant',
										async: true,
										data: {
											id1: reply.rid,
											id2: id
										},
										success: function(xhr) {
											let arg2 = JSON.parse(xhr.responseText);
											if(arg2.code == 300) {
												toast('系统繁忙，请刷新页面后重试');
												setTimeout(() => {
													location.reload(true);
												}, 3000);
											} else if(arg2.code == 3020) {
												location.replace('/page/user/login.html');
											} else if(arg2.code == 200) {
												removeWorld(replyLi);
												toast('删除成功', '#84cc77');
												// 回复数减1
												let count = Number(_this.innerText);
												_this.innerText = count - 1;
											}
										}
									});
								}
							}
							commentReplyUl.appendChild(replyLi);
						});
					}
				} else {
					toast('系统繁忙，请刷新页面后重试');
					setTimeout(() => {
						location.reload(true);
					}, 3000);
				}
			}
		});
	} else {
		publish.style.display = 'none';
		button.onclick = null;
	}
}
}
/*
	功能：决定一开始时显示哪个页面，在url后面追加内容来表示显示哪个子页面
*/
function initInfo() {
	// 默认显示个人资料
	let path = location.href;
	if(path.lastIndexOf('/page/user/space/#/publish') != -1) {
		menuPublishClick();
	} else if(path.lastIndexOf('/page/user/space/#/manage') != -1) {
		menuManageClick();
	} else if(path.lastIndexOf('/page/user/space/#/reply') != -1) {
		menuReplyClick();
	} else if(path.lastIndexOf('/page/user/space/#/like') != -1) {
		menuLikeClick();
	} else if(path.lastIndexOf('/page/user/space/#/my') != -1) {
		menuMyClick();
	} else if(path.lastIndexOf('/page/user/space/#/want') != -1) {
		menuWantClick();
	} else {
		menuDataClick();
	}
}
initInfo();
menuData.onclick = menuDataClick;
menuPublish.onclick = menuPublishClick;
menuManage.onclick = menuManageClick;
menuReply.onclick = menuReplyClick;
menuLike.onclick = menuLikeClick;
menuMy.onclick = menuMyClick;
menuWant.onclick = menuWantClick;

/* info-publish自定义标签 输入框内容改变时输入框宽度动态发生变化
	input.onkeydown = function(e) {
		// 文本内容的长度不能超过10
		if(this.value.length >= 10 && e.keyCode != 8 && e.keyCode != 13 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40) {
			return false;
		} else {
			if(e.keyCode == 8) {
				if(this.value.length * 12 - 70 < this.scrollWidth) {
					if(this.scrollWidth <= 109) {
						this.style.width = '104px';
					} else {
						this.style.width = this.scrollWidth - 5 + 'px';
					}
				}
			} else if(e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40){
				if(this.value.length * 14 + 35 >= this.scrollWidth) {
					this.style.width = this.scrollWidth + 14 + 'px';
				}
			}
		}
	}
	input.onkeyup = function() {
		if(this.value.length == 0) {
			this.style.width = '104px';
		}
	}
*/